package net.pimpas.web.tests.servlet;

import java.security.MessageDigest;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.pimpas.operation.exception.OperationException;
import net.pimpas.operation.operation.OperationAnnotation;
import net.pimpas.operation.persistence.operation.OperationPersistenceAbstract;
import net.pimpas.operation.persistence.serviceprovider.OperationPersistenceServiceProvider;
import net.pimpas.operation.persistence.serviceprovider.OperationPersistenceServiceProviderFactoryImpl;
import net.pimpas.web.tests.entitys.user.User;

public class HibernateServletInitialize implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent context) {
		
	}

	@SuppressWarnings("unchecked")
	public void contextInitialized(ServletContextEvent context) {
		OperationPersistenceServiceProvider op = OperationPersistenceServiceProviderFactoryImpl.getOperationPersistenceServiceProvider();
		
		try {
			op.call(new TesteOperationHibernate());
		} catch (OperationException e) {
			e.printStackTrace();
		}
	}
	
	@OperationAnnotation(name="TesteOperationHibernate")
	public static class TesteOperationHibernate extends OperationPersistenceAbstract {
		
		public TesteOperationHibernate() {
			
		}

		public void commit() throws OperationException {
			
		}

		public void end() throws OperationException {
			
		}

		public void init(Object... params) throws OperationException {
			
		}

		public void rollBack() throws OperationException {
			
		}

		public void start() throws OperationException {
			
		}

		public Object call() throws Exception {
			User pc = new User();
			pc.setLogin("pc");
			MessageDigest md = MessageDigest.getInstance("MD5");
			pc.setPassword(new String(md.digest("a".getBytes())));
			pc.setName("Paulo Cesar");
			pc.setLastName("Silva Reis");
			super.getSession().persist(pc);
			
			return pc;
		}
	}
}